<template>
  <!-- 新增修改界面 -->
  <el-dialog title="修改密码" width="50%" :visible.sync="dialogVisible" :close-on-click-modal="false" :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :size="size" @keyup.enter.native="submitForm"
      label-width="100px" style="text-align:left;">
      <el-form-item label="原始密码" prop="oldPassword">
        <el-input type="password" v-model="dataForm.oldPassword" ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="dataForm.password" ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="password2">
        <el-input type="password" v-model="dataForm.password2" ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button  @click="cancelDialog" :size="size">取消</el-button>
      <el-button  type="primary" @click="submitForm" :loading="dialogLoading" :size="size">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { setAdminPassword } from '@/api/index'
import Cookies from 'js-cookie'
export default {
  props: {
    userId: {
      type: String,
      default: ''
    }
  },
  data () {
    let validateConfirmPassword = (rule, value, callback) => {
      if (this.dataForm.password !== value) {
        callback(new Error('确认密码与新密码不一致'))
      } else {
        callback()
      }
    }
    let checkPassword = (rule, value, callback) => {
      // 8-16 位，字母、数字、字符
      let pwdRegex = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,16}$/
      if (!pwdRegex.test(value)) {
        callback(new Error('密码长度为8-20且必须包含大、小写字母及数字'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      dialogLoading: false,
      dataForm: {
        oldPassword: '',
        password: '',
        password2: ''
      },
      size: 'small',
      dataRule: {
        password: [
          {required: true, message: '新密码不能为空', trigger: 'blur'},
          {validator: checkPassword, trigger: 'change'}
        ],
        password2: [
          {required: true, message: '确认密码不能为空', trigger: 'change'},
          {validator: validateConfirmPassword, trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    init (flag) {
      this.dialogVisible = flag
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    cancelDialog () {
      this.dialogVisible = false
    },
    submitForm () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            // this.dataForm.userId = this.userId
            setAdminPassword(this.dataForm).then((res) => {
              if (res.code === 200) {
                this.dialogVisible = false
                this.clearData()
              }
            })
          })
        }
      })
    },
    // 清除数据
    clearData () {
      Cookies.remove('token')
      Cookies.remove('authorization')
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
